package com.junzhiit.nengji.app.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.app.service.RoleService;
import com.junzhiit.nengji.app.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.app.web.vo.RoleAEVO;
import com.junzhiit.nengji.app.web.vo.RoleFunctionAddVO;
import com.junzhiit.nengji.app.web.vo.RoleListVO;
import com.junzhiit.nengji.app.web.vo.RoleVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;

	@ApiOperation(value = "获取所有角色列表")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findAll(DataTablesParamVO dtp, RoleVO roleVO) {
		return roleService.findAll(dtp, roleVO);
	}

	@ApiOperation(value = "根据ID查询角色")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResultVO findById(@PathVariable String id) {
		AssertUtil.notBlank(id, "id is required.");
		return roleService.findById(id);
	}

	@ApiOperation(value = "创建角色")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO add(RoleAEVO roleVO) {
		AssertUtil.notBlank(roleVO.getName(), "name is required.");
		return roleService.add(roleVO);
	}

	@ApiOperation(value = "修改角色")
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public ResultVO update(@PathVariable String id, RoleAEVO roleAE) {
		AssertUtil.notBlank(roleAE.getName(), "name is required.");
		return roleService.edit(id, roleAE);
	}

	@ApiOperation(value = "删除角色")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public ResultVO delete(String id) {
		AssertUtil.notBlank(id, "id is required.");
		return roleService.delete(id);
	}

	@ApiOperation(value = "获取角色使用个数")
	@RequestMapping(value = "/used/count", method = RequestMethod.GET)
	public ResultVO getCountByRoleId(String id) {
		ResultVO result = new ResultVO();
		result.setData(roleService.getCountByRoleId(id));
		return result;
	}

	@ApiOperation(value = "获取到所有的角色")
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public ResultVO getAll(String sessionId) {
		return roleService.getAll(sessionId);
	}

	@ApiOperation(value = "获取到所有的角色(包含功能)")
	@RequestMapping(value = "/all/function", method = RequestMethod.GET)
	public ResultVO getAllWithFunction(RoleListVO roleListVO) {
		return roleService.getAllWithFunction(roleListVO);
	}

	@ApiOperation(value = "添加角色相关功能")
	@RequestMapping(value = "/function", method = RequestMethod.POST)
	public ResultVO addRoleFunction(RoleFunctionAddVO roleFunctionAddVO) {
		return roleService.addRoleFunction(roleFunctionAddVO);
	}
}
